package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.JsonResult;
import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SystemDictionaryItemObject;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("/list")
    public String list(Model model, SystemDictionaryItemObject qo) {
        PageInfo result = systemDictionaryItemService.query(qo);
        model.addAttribute("pageInfo", result);
        model.addAttribute("qo", qo);
        model.addAttribute("systemDictionaries", systemDictionaryService.list());
        return "systemDictionaryItem/list";
    }

    @RequestMapping("/input")
    public String input(Model model, Long id) {
        if (id != null) {
            model.addAttribute("systemDictionaryItem", systemDictionaryItemService.get(id));
        }
        return "systemDictionaryItem/input";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Model model, Long id) {
        try {
            systemDictionaryItemService.delete(id);
        } catch (Exception e) {
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult save(SystemDictionaryItem systemDictionaryItem) {
        try {
            if (systemDictionaryItem.getId() != null) {
                systemDictionaryItemService.update(systemDictionaryItem);
            } else {
                systemDictionaryItemService.save(systemDictionaryItem);
            }
        } catch (Exception e) {
            return new JsonResult(false, "操作失败");
        }
        return new JsonResult(true, "操作成功");
    }

}
